#! /usr/bin/env python
# encoding=utf-8
import urllib2
import sys
import numpy as np

target_url = ("https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data")
data = urllib2.urlopen(target_url)
xList = []
labels = []
for line in data:
    row = line.strip().split(",")
    xList.append(row)
nrow = len(xList)
ncol = len(xList[1])
sys.stdout.write("Number of Rows of Data = " + str(nrow) + '\n')
sys.stdout.write("Number of Columns of Data = " + str(ncol) + '\n')

type = [0] * 3
colCounts = []

col = 3
colData = []
for row in xList:
    colData.append(float(row[col]))

colArray = np.array(colData)
colMean = np.mean(colArray)
colstd = np.std(colArray)
sys.stdout.write("Mean:" + '\t' + "Std:" + '\t' + "Other\n")
sys.stdout.write(str(colMean) + '\t\t' + str(colstd))
# sys.stdout.write("Col#" + '\t' + "Number" + '\t' + "Strings" + '\t' + "Other\n")

ntiles = 4

percentBdry = []

for i in range(ntiles):
    percentBdry.append(np.percentile(colArray, (i+1)*(100)/ntiles))


sys.stdout.write("\nBoundaries for 4 Equal Percentiles \n")
print(percentBdry)
sys.stdout.write(" \n")


ntiles = 10

percentBdry = []

for i in range(ntiles):
     percentBdry.append(np.percentile(colArray, (i+1)*(100)/ntiles))

sys.stdout.write("Boundaries for 10  Equal Percentiles \n")
print(percentBdry)
sys.stdout.write(" \n")

col = 60
colData = []
for row in xList:
    colData.append(row[col])

unique = set(colData)
sys.stdout.write("Unique Label Values \n")
print(unique)

catDict = dict(zip(list(unique), range(len(unique))))
catCount = [0]*2

for elt in colData:
    catCount[catDict[elt]] += 1
sys.stdout.write("\n Counts for Each Value of Categorical Label \n")
print(list(unique))
print(catCount)

